package com.example.drawerdemo

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.view_holder.view.*

/**
 * recyclerView的数据适配器
 */
class MyListAdapter(val isPager:Boolean) :ListAdapter<Int,RecyclerView.ViewHolder>(callback){

    object callback: DiffUtil.ItemCallback<Int>() {
        override fun areItemsTheSame(oldItem: Int, newItem: Int): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: Int, newItem: Int): Boolean {
            return oldItem == newItem
        }

    }

    //创建单个项目
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.view_holder,parent,false)
        //ViewHolder(view)为抽象类，不能直接实例化，以前采用继承其的子类来实例化
        //return RecyclerView.ViewHolder(view)
        //通过匿名内部类解决上述问题
        if(isPager){
            view.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
            //图标填满
            view.cellImageView.layoutParams.width = 0
            view.cellImageView.layoutParams.height = 0

        }
        return object :RecyclerView.ViewHolder(view){}

    }

    //绑定数据（图标）
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        holder.itemView.cellImageView.setImageResource(getItem(position))
    }
}